package edu.nwpu.zya.coreops.modules.alert.service.impl;

import cn.hutool.core.date.DateUtil;
import edu.nwpu.zya.coreops.common.utils.Constant;
import edu.nwpu.zya.coreops.common.utils.DateUtils;
import edu.nwpu.zya.coreops.modules.alert.entity.RuleEntity;
import edu.nwpu.zya.coreops.modules.alert.service.RuleService;
import io.swagger.models.auth.In;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.nwpu.zya.coreops.common.utils.PageUtils;
import edu.nwpu.zya.coreops.common.utils.Query;

import edu.nwpu.zya.coreops.modules.alert.dao.HistoryDao;
import edu.nwpu.zya.coreops.modules.alert.entity.HistoryEntity;
import edu.nwpu.zya.coreops.modules.alert.service.HistoryService;
import org.springframework.util.StringUtils;


@Service("historyService")
public class HistoryServiceImpl extends ServiceImpl<HistoryDao, HistoryEntity> implements HistoryService {
    @Autowired
    private RuleService ruleService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Long userId = (Long) params.get("userId");
        String ruleId = (String) params.get("ruleId");
        String instanceId = (String) params.get("instanceId");
        String type = (String) params.get("type");

        QueryWrapper<HistoryEntity> queryWrapper = new QueryWrapper<>();

        // 封装查询条件
        if (!StringUtils.isEmpty(instanceId)) {
            queryWrapper.eq("instance_id", instanceId);
        }
        if (!StringUtils.isEmpty(type)) {
            queryWrapper.eq("type", Integer.parseInt(type));
        }
        if (!StringUtils.isEmpty(ruleId)) {
            queryWrapper.eq("rule_id", Long.parseLong(ruleId));
        }
        if (userId != Constant.SUPER_ADMIN) {
            List<RuleEntity> ruleEntities = ruleService.list(new QueryWrapper<RuleEntity>().eq("user_id", userId));
            List<Long> ruleIds = ruleEntities.stream().map(item -> {
                return item.getId();
            }).collect(Collectors.toList());
            queryWrapper.in("rule_id", ruleIds);
        }

        IPage<HistoryEntity> page = this.page(
                new Query<HistoryEntity>().getPage(params),
                queryWrapper.orderByDesc("alert_time")
        );
        List<HistoryEntity> historyEntities = page.getRecords();

        return new PageUtils(page);
    }

    @Override
    public List<HistoryEntity> getHistoryByInstanceId(String instanceId) {
        QueryWrapper<HistoryEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("instance_id", instanceId).orderByDesc("alert_time");
        queryWrapper.last("limit 10");
        List<HistoryEntity> historyEntities = this.list(queryWrapper);
        return historyEntities;
    }

    @Override
    public List<HistoryEntity> getHistoryByRuleId(Long ruleId) {
        QueryWrapper<HistoryEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("rule_id", ruleId).orderByDesc("alert_time");
        queryWrapper.last("limit 10");
        List<HistoryEntity> historyEntities = this.list(queryWrapper);
        return historyEntities;
    }

    @Override
    public List<HistoryEntity> getHistoryByUserId(Long userId) {
        QueryWrapper<RuleEntity> queryWrapper = new QueryWrapper<>();
        if (userId.intValue() != Constant.SUPER_ADMIN) {
            queryWrapper.eq("user_id", userId);
        }
        List<Long> ruleIds = ruleService.list(queryWrapper).stream().map(item -> {
            return item.getId();
        }).collect(Collectors.toList());

        if (ruleIds.size() <= 0) {
            return new ArrayList<HistoryEntity>();
        } else {
            // 获取今日告警历史
            Date endTime=new Date();
            Date startTime = DateUtil.beginOfDay(endTime).toJdkDate();
            return this.list(new QueryWrapper<HistoryEntity>().in("rule_id", ruleIds).between("alert_time",startTime,endTime).orderByDesc("alert_time").last("limit 50"));
        }
    }

}